//
//  QueryDetailController.m
//  shike
//
//  Created by RegentSoft on 2017/4/25.
//  Copyright © 2017年 RegentSoft. All rights reserved.
//

#import "QueryDetailController.h"
#import "QueryDetailCell.h"
@interface QueryDetailController ()<UITableViewDelegate,UITableViewDataSource>{
    UIView * headView;
    UITableView *testTabView;
    NSArray * array;
    NSInteger  indexInteger;
    NSInteger  secondsCountDown; //考试时间
    UILabel * numlabel;          //当前考试的题目
    UILabel * timelabel;
    
}
@property (strong, nonatomic) NSMutableArray * answerArray;//存放考试答案
@property (strong, nonatomic) NSTimer * countDownTimer;//倒计时
@property (assign, nonatomic) BOOL  hidden;//当前题目选择的答案
@end
@implementation QueryDetailController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"技巧销售考核";
    self.view.backgroundColor = [UIColor whiteColor];
    indexInteger = 0;
    secondsCountDown = 120;
    array =[[NSArray alloc] initWithObjects:@"A",@"B",@"C",@"D",@"E",@"F", nil];
    [self creatView];
}
-(void)creatView{
    
    headView =[[UIView alloc] initWithFrame:CGRectMake(0,0, DeviceWidth, DeviceHeight-50)];
    headView.backgroundColor = [UIColor colorWithHexString:mainBgColor];
    [self.view addSubview:headView];
    testTabView = [[UITableView alloc] initWithFrame:CGRectMake(0,0, DeviceWidth, 360) style:UITableViewStylePlain];
    testTabView.delegate = self;
    testTabView.dataSource = self;
    testTabView.contentOffset = CGPointMake(DeviceWidth*3,DeviceHeight-50);
    testTabView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [headView addSubview:testTabView];
    UIView * footView =[[UIView alloc] initWithFrame:CGRectMake(0, DeviceHeight-50, DeviceWidth, 50)];
    footView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:footView];
    
    UIView * lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, DeviceWidth, 1)];
    lineView.backgroundColor = [UIColor blackColor];
    lineView.alpha = 0.3;
    [footView addSubview:lineView];
    
    UIButton * nextButton =[[UIButton alloc] initWithFrame:CGRectMake(20, 10, 70, 30)];
    nextButton.backgroundColor = [UIColor colorWithHexString:@"#ffffff"];
    [nextButton setTitleColor:[UIColor colorWithHexString:@"#c8c8c8"] forState:UIControlStateNormal];
    nextButton.alpha = 1.0;
    [nextButton setTitle:@"上一题" forState:UIControlStateNormal];
    nextButton.titleLabel.font = [UIFont systemFontOfSize:14.0];
    nextButton.tag = 1000;
    nextButton.layer.borderWidth = 1.0;
    nextButton.layer.borderColor = [[UIColor colorWithHexString:@"#979797"] CGColor];
    nextButton.layer.cornerRadius = 8.0;
    [nextButton addTarget:self action:@selector(nextBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [footView addSubview:nextButton];
    
    UIButton * intupButton =[[UIButton alloc] initWithFrame:CGRectMake(DeviceWidth-90, 10, 70, 30)];
    intupButton.backgroundColor = [UIColor colorWithHexString:@"#ffffff"];
    [intupButton setTitleColor:[UIColor colorWithHexString:@"#c8c8c8"] forState:UIControlStateNormal];
    [intupButton setTitle:@"下一题" forState:UIControlStateNormal];
    intupButton.titleLabel.font = [UIFont systemFontOfSize:14.0];
    intupButton.layer.borderWidth = 1.0;
    intupButton.layer.borderColor = [[UIColor colorWithHexString:@"#979797"] CGColor];
    intupButton.layer.cornerRadius = 8.0;
    intupButton.tag = 1001;
    [intupButton addTarget:self action:@selector(nextBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [footView addSubview:intupButton];
    
    numlabel = [[UILabel alloc] initWithFrame:CGRectMake(footView.center.x-20, 15,40,20)];
    numlabel.text = @"10/12";
    numlabel.textColor = [UIColor colorWithHexString:@"#323232"];
    numlabel.font = [UIFont systemFontOfSize:16.0];
    numlabel.textAlignment = NSTextAlignmentCenter;
    [footView addSubview:numlabel];
    
    [self answerView];
}
-(void)answerView{

    UILabel * answerLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 365, DeviceWidth, 30)];
    answerLabel1.backgroundColor = [UIColor whiteColor];
    answerLabel1.text = @"    选择：A";
    answerLabel1.textColor = [UIColor colorWithHexString:@"#323232"];
    answerLabel1.textAlignment = NSTextAlignmentLeft;
    answerLabel1.font = [UIFont systemFontOfSize:16.0];
    [headView addSubview:answerLabel1];
    
    UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(DeviceWidth-70, 365, 50, 30)];
    imageView.image = [UIImage imageNamed:@"errorlogo"];
    [headView addSubview:imageView];
    
    UILabel * answerLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(0, 400, DeviceWidth, 30)];
    answerLabel2.backgroundColor = [UIColor whiteColor];
    answerLabel2.text = @"    正确答案：B";
    answerLabel2.textColor = [UIColor colorWithHexString:@"#323232"];
    answerLabel2.textAlignment = NSTextAlignmentLeft;
    answerLabel2.font = [UIFont systemFontOfSize:16.0];
    [headView addSubview:answerLabel2];
    
    UIView * view =[[UIView alloc] init];
    view.backgroundColor = [UIColor whiteColor];
    [headView addSubview:view];
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(0);
        make.right.equalTo(self.view.mas_right).offset(0);
        make.top.equalTo(answerLabel2.mas_bottom).offset(0);
        make.bottom.equalTo(self.view.mas_bottom).offset(-50);
    }];
    
    UILabel * answerLabel3 = [[UILabel alloc] init];
    answerLabel3.backgroundColor = [UIColor whiteColor];
    answerLabel3.text = @"答案解析";
    answerLabel3.textColor = [UIColor colorWithHexString:@"#999999"];
    answerLabel3.textAlignment = NSTextAlignmentLeft;
    answerLabel3.font = [UIFont systemFontOfSize:14.0];
    [view addSubview:answerLabel3];
    
    UILabel * answerLabel4 = [[UILabel alloc] init];
    answerLabel4.backgroundColor = [UIColor whiteColor];
    answerLabel4.text = @"歌莉娅是在2001创立的！歌莉娅是在2001创立的！歌莉娅是在2001创立的！重要的事情说3遍";
    answerLabel4.numberOfLines = 0;
    for(int i=0; i<8; i++){
        answerLabel4.text = [answerLabel4.text stringByAppendingString:@"\n "];
    }
    answerLabel4.textColor = [UIColor colorWithHexString:@"#323232"];
    answerLabel4.textAlignment = NSTextAlignmentLeft;
    answerLabel4.font = [UIFont systemFontOfSize:14.0];
    [view addSubview:answerLabel4];
    
    [answerLabel3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(20);
        make.right.equalTo(self.view.mas_right).offset(-20);
        make.top.equalTo(view.mas_top).offset(5);
        make.height.mas_equalTo(15);
    }];
    [answerLabel4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left).offset(20);
        make.right.equalTo(self.view.mas_right).offset(-20);
        make.top.equalTo(answerLabel3.mas_bottom).offset(0);
        make.bottom.equalTo(view.mas_bottom).offset(0);
    }];
}
-(void)nextBtnClick:(UIButton *)sender{
    
    
}
-(void)submitClic{
    
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    NSString * titleString;
    NSString * numberString;
    if (section==0) {
        titleString = @"题目(单选题)";
        numberString= @"10";
    }else{
        titleString = @"选项";
    }
    UIView * titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, DeviceWidth, 30)];
    titleView.backgroundColor =[UIColor whiteColor];
    UILabel * titleLabel =[[UILabel alloc] initWithFrame:CGRectMake(20, 5, 100, 20)];
    titleLabel.textColor = [UIColor colorWithHexString:@"#999999"];
    titleLabel.textAlignment = NSTextAlignmentLeft;
    titleLabel.font = [UIFont systemFontOfSize:12.0];
    titleLabel.text = titleString;
    [titleView addSubview:titleLabel];
    
    UILabel * numberLabel = [[UILabel alloc] initWithFrame:CGRectMake(DeviceWidth-40, 5, 20, 20)];
    numberLabel.textColor = [UIColor colorWithHexString:@"#999999"];
    numberLabel.textAlignment = NSTextAlignmentCenter;
    numberLabel.font = [UIFont systemFontOfSize:12.0];
    numberLabel.text = numberString;
    [titleView addSubview:numberLabel];
    return titleView;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 30;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==0) {
        return 80;
    }else{
        return 40;
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section ==0) {
        return 1;
    }else{
        return 4;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString * cellIdenifer = @"idenifer";
    QueryDetailCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdenifer];
    if (!cell) {
        cell =[[QueryDetailCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdenifer];
    }
    if (indexPath.section==1) {
        cell.titleLabel.text = [NSString stringWithFormat:@"%@",[array objectAtIndex:indexPath.row]];
        cell.selectBtn.tag = indexPath.row+1000;
        cell.selectBtn.selected = NO;
        //单选的做法
        if (cell.selectBtn.tag == indexInteger) {
            [cell.selectBtn setImage:[UIImage imageNamed:@"selected"] forState:UIControlStateNormal];
            NSMutableDictionary * answerDic = [[NSMutableDictionary alloc] init];
            NSNumber *boolNumber = [NSNumber numberWithBool:_hidden];
            [answerDic setValue:boolNumber forKey:@"cell.contentLabel.text"];
            [_answerArray addObject:answerDic];
        }else{
            [cell.selectBtn setImage:[UIImage imageNamed:@"unSelect"] forState:UIControlStateNormal];
        }
        cell.contentLabel.text = @"销售技巧考核";
        [cell.selectBtn addTarget:self action:@selector(chooseBtn:) forControlEvents:UIControlEventTouchUpInside];
    }else{
        cell.selectBtn.hidden = YES;
        cell.headLabel.text = @"1.歌莉娅是在哪年创立的？歌莉娅是在哪年创立的？歌莉娅是在哪年创立的？歌莉娅是在哪年创立的？歌莉娅是在哪年创立的？歌莉娅是在哪年创立的？歌莉娅是在哪年创立的？";
    }
    return cell;
}
-(void)chooseBtn:(UIButton *)sender{
    indexInteger =sender.tag;
    [testTabView reloadData];
}
@end
